<?php

namespace App\Admin\Controllers;

use App\Models\Goods;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class GoodsController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('商品');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('商品');
            $content->description('修改');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('商品');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Goods::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            $grid->model()->where([['is_delete', '=', 0]]);
            $grid->id('ID')->sortable();
            $grid->title('标题');
            $grid->desc('描述');
            $grid->main_pic('商品主图')->image();
            $grid->all_pics('全部图片')->image();
            $grid->present_price('价格(元)');
            $grid->type('类型')->display(function ($type) {
               return $type == 1 ?  "<span class=\"label label-info\">单品</span>" :"<span class=\"label label-success\">套餐</span>";
            });
            $grid->group_type('套餐类型')->display(function ($group_type) {
                if ($group_type == 1) {
                    return "<span class=\"label label-primary\">减脂</span>";
                } elseif ($group_type == 2) {
                    return "<span class=\"label label-warning\">增肌</span>";
                } else {
                    return "";
                }
            });
            $grid->is_fresh('是否为新鲜食材')->display(function ($is_fresh) {
                return $is_fresh == 1 ?  "<span class=\"label label-danger\">是</span>" :"<span class=\"label label-primary\">否</span>";
            });
            $grid->status('状态')->switch([
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '下架', 'color' => 'danger'],
            ]);
            $grid->created_at('创建时间');

            $grid->disableExport();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
            $grid->filter(function ($filter) {
                $filter->like('title', '名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        Admin::script($this->script());
        return Admin::form(Goods::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->hidden('id');
            $form->text("title", '商品标题')->rules('required',[
                'required' => '商品标题不能为空'
            ]);
            $form->text('desc', '商品描述')->rules('required',[
               'required' => '商品描述不能为空'
            ]);
            $form->editor('content', '商品内容')->rules('required', [
                'required' => '商品内容不能为空'
            ]);
            $form->currency('present_price', '商品价格')->symbol('￥')->rules('required', [
                'required' => '商品价格不能为空'
            ]);
            $form->image('main_pic', '商品主图')->removable()->uniqueName()->rules('required', [
                'required' => '商品主图不能为空'
            ]);
            $form->multipleImage('all_pics', '全部图片')->removable()->uniqueName()->rules('required', [
                'required' => '全部图片不能为空'
            ]);
            $form->switch('type', '商品类型')->states([
                'on'  => ['value' => 1, 'text' => '单品', 'color' => 'info'],
                'off' => ['value' => 2, 'text' => '套餐', 'color' => 'success'],
            ])->default(1);
            $form->switch('group_type', '套餐类型')->states([
                'on'  => ['value' => 1, 'text' => '减脂', 'color' => 'primary'],
                'off' => ['value' => 2, 'text' => '增肌', 'color' => 'warning'],
            ])->default(1);
            $form->switch('is_fresh', '是否为新鲜食材')->states([
                'on'  => ['value' => 0, 'text' => '否', 'color' => 'primary'],
                'off' => ['value' => 1, 'text' => '是', 'color' => 'warning'],
            ])->default(0);
            $form->switch('status', '状态')->states([
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '下架', 'color' => 'danger'],
            ])->default(1);
            $form->saving(function (Form $form) {

                $id = $form->id;
//                $pics = $form->all_pics;
//                if (is_array($pics)) {
//                    $form->all_pics = json_encode($pics);
//                }
                $where = [['title', '=', $form->title], ['is_delete', '=', '0']];
                if (!empty($id)) {
                    $where[] = ['id', '<>', $id];
                }
                $is_exist = Goods::where($where)->first();
                if (!empty($is_exist)) {
                    $error = new MessageBag([
                        'title'   => '错误',
                        'message' => '商品名称已存在',
                    ]);
                    return back()->with(compact('error'));
                }
            });
        });

    }

    protected function script()
    {

        return <<<EOT
        var type = $('.type').val();
        if (type == 'on') {
            $('.group_type').hide();
        } else {
             $('.group_type').show();
        }     
EOT;
    }
}
